package com.mango.ch09;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.mango.HDFSTools.HDFSAPI;
import com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider.Text;

/**
 * 
 * @author Mango
 *
 */
// com.mango.ch09.groomFriendJob
public class groomFriendJob {

	static Path inputPath = new Path("/groomFriendJob/input");// input
	static Path outPath = new Path("/groomFriendJob/output"); // output

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		// 创建输入文件夹
		Configuration conf = new Configuration();
		try {
			HDFSAPI hdfs = new HDFSAPI(conf);
			hdfs.createDirectory(inputPath);
			// 先删除已经有的输出文件夹
			hdfs.delDirectory(outPath);
			hdfs.closeFilseSystem();
			// hdfs.orpOver();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("----------文件操作失败");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Job job = Job.getInstance(conf, "groomFriendJob");
		job.setJarByClass(groomFriendJob.class);

		job.setMapOutputKeyClass(LongWritable.class);
		job.setMapOutputValueClass(MyTuple.class);

		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		job.setNumReduceTasks(3);

		job.setMapperClass(GroomFriendMapper.class);
		job.setReducerClass(GroomFriendReducer.class);

		FileOutputFormat.setOutputPath(job, outPath);
		FileInputFormat.setInputPaths(job, inputPath);

		System.out.println(job.waitForCompletion(true) ? 0 : 1);
	}
}
